<?php
namespace Api\Controller;
use Think\Controller;
class RegController extends Controller {

	private $m = NULL;
	private $navName = '测试账户录入';
        
        /*  2017-06-14  liuq
	 *  用户注册
	 *
	 */
  
        //手机验证码 - 注册
        public  function  reg_one(){
            
               $phone = I('phone');
               $code = I('code');
               if($code == '' || $phone == ''){
                   returnmemberError("1", "您的信息没有填写完整");exit;
               }
               //防短信轰炸机
               if(empty($_SESSION['sms_mobile'])){exit('非法请求');}
               
                $is_phone = M('member')->where('phone="'.trim(I('phone')).'"')->getField('id');
                if($is_phone>0){
                    returnmemberError("1", "该电话已经被注册过");exit;
                }
                
                
                if( $code !=  $_SESSION['sms_mobile_code'] ){
                    returnmemberError("1", "验证码填写错误");exit;
                }  else {
                   returnApiresult("0", "成功");exit;
                }
                
            
        }
        
        
        
        public function reg_second(){
            $phone = I('phone');//前段存
            $password = I('password');
            $twicepass = I('twicepass');
            if($password == '' || $twicepass == ''){
                   returnmemberError("1", "您的信息没有填写完整");exit;
             }
             
             if($twicepass != $password){
                   returnmemberError("1", "两次输入的密码不相符");exit;
             }else{
             $info['phone'] =  $phone;
             $info['password'] = md5($password);
             $info['add_time']  = time();
             $info['mem_type'] = 2;
             $newId = M('member')->add($info);
                if($newId){
                      $data['cid'] = $newId;//生成新用户ID
                      returnApiData('0','注册成功',$data);exit;		
                }else{
                     returnmemberError("1", "注册失败");exit;
                }
             }
         }
         
         
         public  function  reg_detail(){
             $info['cid'] = I('cid');//前段存   $data['cid']
             
             $info['nickname'] = I('nickname');
             $info['phone'] = I('phone');
             $info['major'] = I('major');
             
             $info['cus_prv'] = I('prv_id');
             $info['cus_city'] = I('city_id');
             $info['cus_school'] = I('school_id');
             
             //$info['std_level'] = I('std_level');
             if( I('std_level') == '大一'){
                 $info['std_level'] = 1;
             }
             
              if( I('std_level') == '大二'){
                 $info['std_level'] = 2;
             }
             
              if( I('std_level') == '大三'){
                 $info['std_level'] = 3;
             }
             
              if( I('std_level') == '大四'){
                 $info['std_level'] = 4;
             }
             
             if($info){
                  $result = M('customer')->add($info);
                  if( $result ){
                       returnApiresult("0", "成功");exit;
                  }
                  
             }
             
             
             
         }
         

         
        //获取短信验证码  并存到session中
        	public function sms(){
			$mobile = I('phone');
			//$mobile = "13624012805";
			if(empty($mobile)){
			  returnmemberError("1", "电话号码不能为空");exit;
			}
		        $sms=$this->send_phone($mobile);
                        //$rcode=$sms[0];  //返回状态值
                        //  if( $rcode ==22){
			//   exit('1小时内同一手机号发送次数超过限制');
			//   }
			// $code=(int)$sms[1];   //返回验证码随机6位
		       if($sms){
                             returnApiresult('0','发送成功');exit;
                       }else{
                             returnmemberError("1", "发送失败");exit;
                        }
                        
		       
		
		}
                
      
                
      public function send_phone($tell){
        import('Org.Alidayu.top.TopClient');
        import('Org.Alidayu.top.ResultSet');
        import('Org.Alidayu.top.RequestCheckUtil');
        import('Org.Alidayu.top.TopLogger');
        import('Org.Alidayu.top.request.AlibabaAliqinFcSmsNumSendRequest');
        $c = new \TopClient;
        $c->appkey = '23699407';  
        $c->secretKey = '9db1f1f2185271c0ed07f5aed816fce1';
        $req = new \AlibabaAliqinFcSmsNumSendRequest;
        $req ->setExtend( "123456" );
        $req ->setSmsType( "normal" );
        $req ->setSmsFreeSignName( "学生惠" );//来源于配置短信签名 下面列表中有签名名称
        //$code = '1234';
        $code= rand(100000,999999);//随机六位验证码
        $_SESSION['sms_mobile_code'] = $code;
        $_SESSION['sms_mobile'] =  $tell;
        $product = '学生惠';
        $req ->setSmsParam( "{code:'{$code}',product:'{$product}'}" ); //变量来源于 配置短信模板 点击列表中的详情 模板内容的变量
        $req ->setRecNum( $tell ); //手机号
        $req ->setSmsTemplateCode("SMS_55690072"); //配置短信模板 列表中有模板id
        $resp = $c ->execute( $req );
        //var_dump($resp);
        return $resp;
    }
                
                
    // {"alibaba_aliqin_fc_sms_num_send_response":{"result":{"err_code":"0","model":"108105986691^1110874033468","success":true},"request_id":"2ex2i7knjw0w"}}           
                
                
                
                
                
	public function reghx($nickname){ 
	          $Hxclass = new \Myself\Hxcall; 
		      $hx_username=date('Ymd').GetfourtwousernameStr_hx(24);
		      $hx_pass=GetfourtwousernameStr_hx(6);
		      $data=$Hxclass->hx_register($hx_username,$hx_pass,$nickname);  //环信通讯注册
		      $info= json_decode($data,true);
			  if($info['error']=='duplicate_unique_property_exists'){
			   return $this->reghx($nickname);
			  }
              if(!$info['error']){
			  $nameinfo=array("hx_name"=>$hx_username,"hx_pass"=>$hx_pass);
			  }
            return $nameinfo;
		}
		

	
	public  function stime_check($birthday){    //生日换成成年龄
	$age = strtotime($birthday); 
	if($age === false){ 
	return false; 
	} 
	list($y1,$m1,$d1) = explode("-",date("Y-m-d",$age)); 
	$now = strtotime("now"); 
	list($y2,$m2,$d2) = explode("-",date("Y-m-d",$now)); 
	$age = $y2 - $y1; 
	if((int)($m2.$d2) < (int)($m1.$d1)) 
	$age -= 1; 
	
	if($age<0){  return  "";  }
	return (int)$age; 
	} 
   
   
	/** 2016-4-18
	* Function reg_one: 添加测试账号，提交接口
	*
	*/	
	public function reg_cs(){
		$is_nickname=M('member_info')->where('nickname="'.trim(I('nickname')).'"')->getField('id');
		if($is_nickname>0){   returnmemberError("1","该昵称已存在，请换一个");exit;  }
/*		if(I('post.stime')){
			  if($this->stime_check(I('post.stime'))<18){
			  returnmemberError("1","未满18岁，不能注册");exit; 
			  }
		}*/
		$data['mobile']=GetfourtwousernameStr_mobile(8);  // 随机登录名数字，代替手机号，无重复
		$data['password']="654321";
/*		$valid=I('post.code')?I('post.code'):"";
		if(!$this->bascCheckValid($valid,$data['mobile'])){  returnApiresult("1","验证码错误");exit;	}*/
		if(empty($data['mobile']) or  empty($data['password']) ){ returnApiresult("1","非法参数");exit;}
		$data['status']=1; //0锁定 1开放
		$data['cs_state']=1; //测试账号标 1
		$data['cs_txt']=I('cs_txt'); //测试账号标 1
		$data['cs_classid']=I('cs_classid'); //测试账号标 1
		$data['city']="沈阳"; //时间
		$data['Admintype']=1; //级别
		$data['addtime']=time(); //添加时间
		$data['username']=GetfourtwousernameStr(8); //默认随机唯一用户名
		$result=$this->md->register($data);
              
		if($result==-1){
		     returnmemberError("1","该测试账号已添加");exit;
		}elseif($result == false){
			 returnmemberError("1","注册失败"); exit;	
		}else{
				$data2["nickname"]=trim(I('post.nickname'));
				$data2["stime"]=I('post.stime')?strtotime(I('post.stime')):"";
				$data2["sex"]=I('post.sex')?I('post.sex'):0;
                                $data2["intro_mem"]=I('post.intro_mem'); //个人简介
				//环信注册
/*				$hxdata=$this->reghx($data2["nickname"]);
				if($hxdata){
				$this->mx->where("mid=".$result)->save($hxdata); //记录环信用户名密码
				}*/
				//环信注册
				$token=M('member_token')->where('mid="'.$result.'"')->getField('token');

				$mapdata=$this->lbs(I('mapx'),I('mapy'),$result,"58");
				

                            if(I('pic')){
				$data2["pic"]= "user/pic/201511/".I('pic');
				}
				
				
				if( !empty($data2["nickname"]) && !empty($data2["stime"]) ){
				$reguesttop =$this->mx->where("mid=".$result)->save($data2); 
				}
				
/*				$info =$this->mx->where("mid=".$result)->field('nickname,stime,sex,pic,hx_name,hx_pass')->find(); 
				$info['mobile']=$data['mobile']; 
				if($info['pic']){ $info['pic']=__VUPLOAD__.$info['pic']; }  */  
				returnApiresult("0","注册成功");exit;
				//returnmemberSuccess("0","注册成功",$result,$token,$info);exit;	
		}
	}		
	
}